<?php
/**
 * Format date/time according to given format string, in the selected language.
 * @param $format date format, as per PHP specs
 * @param $timestamp the timestamp to format
 * @return String with formatted timestamp
 */
function date_format_translated($format, $timestamp) {
	global $translation;

	$day_name_short = array($translation['tstat_su'], $translation['tstat_mo'], $translation['tstat_tu'], $translation['tstat_we'], $translation['tstat_th'], $translation['tstat_fr'], $translation['tstat_sa']);
	$day_name_full = array($translation['tstat_full_su'], $translation['tstat_full_mo'], $translation['tstat_full_tu'], $translation['tstat_full_we'], $translation['tstat_full_th'], $translation['tstat_full_fr'], $translation['tstat_full_sa']);
	$month_name_short = array($translation['tstat_jan'], $translation['tstat_feb'], $translation['tstat_mar'], $translation['tstat_apr'], $translation['tstat_may'], $translation['tstat_jun'],
			$translation['tstat_jul'], $translation['tstat_aug'], $translation['tstat_sep'], $translation['tstat_oct'], $translation['tstat_nov'], $translation['tstat_dec']);
	$month_name_full = array($translation['tstat_full_jan'], $translation['tstat_full_feb'], $translation['tstat_full_mar'], $translation['tstat_full_apr'], $translation['tstat_full_may'], $translation['tstat_full_jun'],
			$translation['tstat_full_jul'], $translation['tstat_full_aug'], $translation['tstat_full_sep'], $translation['tstat_full_oct'], $translation['tstat_full_nov'], $translation['tstat_full_dec']);
		
	// Loop char by char through php date format string, cacth the ones which need translating
	//--- PHP 4 :-)--------------------------------
	if(!function_exists('str_split')){
		function str_split($str,$split_lengt=1){

			$cnt = strlen($str);

			for ($i=0;$i<$cnt;$i+=$split_lengt)
				$rslt[]= substr($str,$i,$split_lengt);
			 
			return $rslt;
		}
	}
	//----------------------------------------------
	$result = "";
	$format_chars = str_split($format, 1);
	foreach ($format_chars as $format_char) {
		if ($format_char == "l") {
			$day_index = date("w", $timestamp);
			$result = $result . $day_name_full[$day_index];
		} else if ($format_char == "D") {
			$day_index = date("w", $timestamp);
			$result = $result . $day_name_short[$day_index];
		} else if ($format_char == "F") {
			$month_index = date("n", $timestamp) - 1;
			$result = $result . $month_name_full[$month_index];
		} else if ($format_char == "M") {
			$month_index = date("n", $timestamp) - 1;
			$result = $result . $month_name_short[$month_index];
		} else {
			$result = $result . date($format_char, $timestamp);
		}
	}

	return $result;
}

// Functions to calculate Stats
function bbc_hgraph_spacer($last, $nr, $values) {
	if ($nr == $last) $str = "none";
	else $str = ($values[$nr] > $values[($nr + 1)]) ? "solid" : "none";

	$str .= " none ";

	if ($nr === 0) $str .= "none";
	else $str .= ($values[$nr] >= $values[($nr - 1)]) ? "solid" : "none";

	return $str;
}

function bbc_plot($x, $y, $width, $height) {
	global $BBC_IMAGES_PATH, $translation;

	// Various sizes
	$nb_x = count($x);
	$nb_y = count($y);
	$nb = !empty($x) ? min($nb_x, $nb_y) : $nb_y;
	$bar_width = round($width / $nb);

	$sum_y = array_sum($y);
	$nb_e = $nb;
	if ($nb_e == 12) {
		for ($i = 0; $i < 12; $i++) {
			if ($y[$i] != 0) {
				$nb_e = $nb_e-$i;
				break;
			}
		}
	}
	$avarage=round($sum_y/$nb_e);

	// Finding the max
	for ($k = 0, $max_y = 0; $k < $nb; $max_y = max($y[$k],$max_y), $k++);
	// The height of one unit
	$unit_height = !empty($max_y) ? (0.92 * ($height / $max_y)) : 0;

	$avarage_height = round($max_y*$unit_height) - round($avarage*$unit_height)+17;
	$str = "<div class=\"hgraph-line\" style=\"top:".$avarage_height."px; \"><p style=\"text-align:center;color:#000; padding:0px;margin:0px;\"><em>Average : ".$avarage."</em></p></div>";
	/*$str  .= "<table class=\"centerbox\">\n"
	."<tr class=\"hgraph-background\">\n";*/
	
	$str .= "<table cellspacing=\"0\"><tr>";
	for ($k = 0; $k < $nb; $k++) {
		$bar_height = round($y[$k] * $unit_height);

		$str .= "<td width=\"{$bar_width}px\" height=\"{$height}px\" title=\"\" class=\"hgraph\">\n";
		

		if ($y[$k]) {
			$numb = ($y[$k] >= 1000) ? substr(($tmp = $y[$k] / 1000) ,0 , (strpos($tmp, ".") + 2))."k" : $y[$k];

			$bbc_column_color = "column";  // default column color
			if ($nb > 27 || $nb == 7) {  // make sure it's a month and not a week, hour, etc.
				$bbc_weekend = mktime(0, 0, 0, date("m"), date("d") - ($nb - $k - 1), date("Y"));
				if (date("w", $bbc_weekend) == 0 || date("w", $bbc_weekend) == 6) {  // 0 is Sunday and 6 is Saturday
					$bbc_column_color = "weekend-column";  // column color for weekends
				}
			} else if ($nb == 24) {
				$bbc_weekend = mktime(date("G") - ($nb - $k - 1), 0, 0, date("m"), date("d"), date("Y"));
			} else {
				$bbc_weekend = mktime(0, 0, 0, date("m") - ($nb - $k - 1), date("d"), date("Y"));
			}
			
			$numb = ($k == ($nb-1)) ? '<strong style="color:#000;">'.$numb.'</strong>' : $numb;
			$str .= "<span style=\"height:".($bar_height)."px;width:".($bar_width-5)."px\"><em>$numb</em></span>";
			/*."style=\"border-style: none ".bbc_hgraph_spacer(($nb - 1), $k, $y)."\">"
			."<span class=\"label-hgraph-ydata\">$numb</span>"
			."</td>\n"
			."</tr>\n"
			."<tr class=\"".$bbc_column_color."\">\n";
			if ($nb > 27 || $nb == 7) {  // make sure it's a month and not a week, hour, etc.
				// week (7 days), and full month (>27 days)
			$str .= "<td title=\"".date_format_translated($translation['global_day_format'], $bbc_weekend)."&nbsp;&laquo;".$y[$k]."&raquo;\" height=\"$bar_height\" class=\"brd\" ";
			} else if ($nb == 24) {
				// hours (24)
				$str .= "<td title=\"".date_format_translated($translation['global_hours_format'], $bbc_weekend)."&nbsp;&laquo;".$y[$k]."&raquo;\" height=\"$bar_height\" class=\"brd\" ";
				} else {
				// months (12)
				$str .= "<td title=\"".date_format_translated($translation['global_month_format'], $bbc_weekend)."&nbsp;&laquo;".$y[$k]."&raquo;\" height=\"$bar_height\" class=\"brd\" ";
				}
				$str .= "style=\"border-style: solid ".bbc_hgraph_spacer(($nb - 1), $k, $y)."\"></td>\n";*/
		}
		else $str .= "<span style=\"height:0\"></span>\n";

		$str .= "</td>\n";
			
		}

		$str .= "</tr>\n"
		."<tbody>\n"
		."<tr>\n";
	
		for ($k = 0; $k < $nb; $k++) {
			$xplot = ($k == ($nb-1)) ? '<strong style="color:#000;">'.$x[$k].'</strong>' : $x[$k];
			$str .= "<td width=\"{$bar_width}px\" class=\"xplot\">".$xplot."</td>\n";
		}

		$str .= "</tr>\n"
		."</tbody>\n"
		."</table>\n";

		//$str .= "</div>";
		return $str;
		}

		
function bbc_show_plot_time_type($time_type, $width, $height) {
	global $BBC_TIMESTAMP, $BBC_TIME_OFFSET, $translation, $access;

	$last_time = isset($access['time']['last']) ? $access['time']['last'] : 0;
	$current_time = $BBC_TIMESTAMP + ($BBC_TIME_OFFSET * 60);
	$nb_seconds_in_day  = 86400;
	$nb_seconds_in_week = 7 * $nb_seconds_in_day;
	$last_month = date("n", $last_time) - 1;
	$nb_seconds_in_last_year = (date("L", $last_time) ? 366 : 365) * $nb_seconds_in_day;

	switch ($time_type) {
		case "hour":
			$current_hour = date("G", $current_time);
			$last_hour    = date("G", $last_time);

			for ($k = $current_hour - 23; $k <= $current_hour; $x[] = ($k < 0) ? ($k + 24) : $k, $k++);
			for ($k = 0; $k < 24; $y[$k] = 0, $k++);
			if (($current_time - $last_time) <= $nb_seconds_in_day) {
				$elapsed = $current_hour - $last_hour;
				$elapsed = ($elapsed < 0) ? ($elapsed + 24) : $elapsed;

				for ($k = $elapsed; $k < 24; $k++) {
					$y[$k - $elapsed] = $access['time']['hour'][($last_hour + 1 + $k) % 24];
				}
			}
			break;

		case "wday":
			$day_name = array($translation['tstat_su'], $translation['tstat_mo'], $translation['tstat_tu'], $translation['tstat_we'], $translation['tstat_th'], $translation['tstat_fr'], $translation['tstat_sa']);

			$current_wday = date("w", $current_time);
			$last_wday    = date("w", $last_time);

			for ($k = $current_wday - 6; $k <= $current_wday;
			$x[] = $day_name[($k < 0) ? $k + 7 : $k], $k++);
			for ($k = 0; $k < 7; $y[$k] = 0, $k++);
			if (($current_time - $last_time) <= $nb_seconds_in_week) {
				$elapsed = $current_wday - $last_wday;
				$elapsed = ($elapsed < 0) ? $elapsed + 7 : $elapsed;

				for ($k = $elapsed; $k < 7; $k++) {
					$y[$k - $elapsed] = $access['time']['wday'][($last_wday + 1 + $k) % 7];
				}
			}
			break;

		case "day":
			// We suppose that the first day of the month is 0 for array compatibility
			$current_day    = date("j", $current_time) - 1;
			$last_day       = date("j", $last_time) - 1;
			$time_in_prec_month = $current_time - ($current_day + 1) * $nb_seconds_in_day;
			$lg_prec_month  = date("t", $time_in_prec_month);
			$lg_prec_month  = ($current_day >= $lg_prec_month) ? ($current_day + 1) : $lg_prec_month;
			$current_month  = date("n", $current_time);
			$prec_month     = date("n", $time_in_prec_month);

			// Computing the $x
			for ($k = $current_day + 1; $k < $lg_prec_month; $x[] = ($k + 1), $k++);
			for ($k = 0; $k <= $current_day; $x[] = ($k + 1), $k++);
			// Computing the $y
			for ($k = 0; $k < 31; $y[$k] = 0, $k++);
			if (($current_time - $last_time) <= ($lg_prec_month * $nb_seconds_in_day)) {
				$elapsed = $current_day - $last_day;
				$elapsed = ($elapsed < 0) ? ($elapsed + $lg_prec_month) : $elapsed;

				for ($k = $elapsed; $k < $lg_prec_month; $k++) {
					$y[$k - $elapsed] = $access['time']['day'][($last_day + 1 + $k) % $lg_prec_month];
				}
			}
			break;

		case "month":
			$month_name = array('1월','2월','3월','4월','5월','6월','7월','8월','9월','10월','11월','12월');

			$current_month = date("n", $current_time) - 1;
			$last_month    = date("n", $last_time) - 1;

			for ($k = $current_month - 11; $k <= $current_month; $x[] = $month_name[(($k < 0) ? ($k + 12) : $k)], $k++);
			for ($k = 0; $k < 12; $y[$k] = 0, $k++);

			if (($current_time - $last_time) <= $nb_seconds_in_last_year) {
				$elapsed = $current_month - $last_month;
				$elapsed = ($elapsed < 0) ? $elapsed + 12 : $elapsed;

				for ($k = $elapsed; $k < 12; $k++) {
					$y[$k - $elapsed] = $access['time']['month'][(($last_month + 1 + $k) % 12)];
				}
			}
			break;
	}
	return bbc_plot($x, $y, $width, $height);
}



?>


<style type="text/css">
.box_type02 table td.hgraph{padding:0;vertical-align:bottom;margin:0;padding:0;}
.box_type02 table td.hgraph>div{height:100%; padding:0;margin:0;text-align;center}
/*.box_type02 table td.hgraph>div>p{width:15px; height:0%;background-color:#fe6c00; display:inline-block;border:1px solid red}*/
.box_type02 table td.hgraph>span{height:0%;background-color:#fe6c00;display:inline-block;position:relative; padding:0;margin:0; }
.box_type02 table td.hgraph>span>em{position:absolute; top:-15px;}
.box_type02 table td.xplot{background:#ccc}
.hgraph-line {
border-bottom: 1px dashed #999;
margin: 0px;
padding: 0px;
background: transparent none repeat scroll 0% 0%;
position: relative;z-index:100;}

.brd {
border-width: 1px;
border-color: #606680}

.hover_bar:hover {
background-color: #e5f2f7;
border: 0px
}
</style>
<div class="box_type02">
	<h2 class="txt">방문자 통계</h2>
	<p class="icon"><a href="javascript:void(0);" class="icon_m04">매뉴얼</a></p>
	<?php 
		echo bbc_show_plot_time_type("hour", 768, 200);
		echo bbc_show_plot_time_type("month", 768, 200);
	?>

</div>
